package com.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.domain.BloodCollectionRecord;
import com.hospital.domain.Hospital;
import com.hospital.domain.Volunteer;
import com.hospital.service.VolunteerService;
import com.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/volunteer")
@CrossOrigin(origins = "*",maxAge = 3600)
public class VolunteerController {
    @Autowired
    private VolunteerService volunteerService;

    @GetMapping("/selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "3") Integer pageSize){
        Page<Volunteer> page = new Page<>(pageNum,pageSize);
        Page<Volunteer> hospitalPage = volunteerService.page(page);
        List<Volunteer> records = hospitalPage.getRecords();
        int total = volunteerService.count();
        return R.ok().put("data",records).put("total",total);
    }

    @PostMapping("save")
    public R save(@RequestBody Volunteer volunteer){
        return R.ok("添加成功").put("list",volunteerService.save(volunteer));
    }

    @PutMapping("/update")
    public R update(@RequestBody Volunteer volunteer) {
            return R.ok("更新成功").put("list",volunteerService.updateById(volunteer));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable int id){
        if(!volunteerService.removeById(id))
            return R.error("删除失败");
        else
            return R.ok("删除成功").put("list",volunteerService.removeById(id));
    }
}
